package com.bearingnavi.android.overlays;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.bearingnavi.android.util.MarshalOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class WalkingPathOverlay extends Overlay implements MarshalOverlay {
	
	private final List<GeoPoint> geoPoints;
	
	public void cutWalkingPath(){
		if (geoPoints.size() == 0) {
			return;
		}
		if (geoPoints.get(geoPoints.size()-1).getLatitudeE6() == -1 && 
				geoPoints.get(geoPoints.size()-1).getLongitudeE6() == -1){
			return;
		}
		geoPoints.add(new GeoPoint(-1, -1));
	}
	
	public void addGeoPoint(GeoPoint geoPoint) {
		geoPoints.add(geoPoint);
	}

	public WalkingPathOverlay() {
		geoPoints = new ArrayList<GeoPoint>();
	}
	
	public void merge(WalkingPathOverlay overlay) {
		cutWalkingPath();
		geoPoints.addAll(overlay.geoPoints);
		cutWalkingPath();
	}
	
	public void clean() {
		geoPoints.clear();
	}

	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);

		Projection projection = mapv.getProjection();
		if (shadow == false && geoPoints != null) {
//			Point startPoint = null;
//			Point endPoint = null;
			Path path = new Path();
			// We are creating the path
			boolean movePathTo = true;
			for (GeoPoint geoPoint : geoPoints) {
				Point point = new Point();
				
				projection.toPixels(geoPoint, point);
				
				if (geoPoint.getLatitudeE6() == -1) {
					movePathTo = true;
					continue;
				}
				
				if (movePathTo) { // This is the start point
//					startPoint = pointA;
					path.moveTo(point.x, point.y);
					movePathTo = false;
				} else {
//					if (i == geoPoints.size() - 1) {// This is the end point
//						endPoint = pointA;
//					}
					path.lineTo(point.x, point.y);
				}
			}
			
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(3);
			
			if (!path.isEmpty()) {
				canvas.drawPath(path, paint);
			}
		}
	}

	public String marshal() {
		if (geoPoints.size() == 0) {
			return null;
		}
		StringBuilder builder = new StringBuilder();
		
		for (GeoPoint geoPoint : geoPoints) {
			builder.append(geoPoint.getLatitudeE6());
			builder.append(MarshalOverlay.FIELDS_SEPARATOR);
			builder.append(geoPoint.getLongitudeE6());
			builder.append(MarshalOverlay.FIELDS_SEPARATOR);
		}
		
		String marshalledData = builder.toString();
		marshalledData = marshalledData.substring(0, marshalledData.length()-MarshalOverlay.FIELDS_SEPARATOR.length());
		return marshalledData;
	}

	public void unmarshal(String[] objectFields, boolean createAsSnapshot, MapView mapView) {
		for (int i = 0; i < (objectFields.length-1)/2; i++) {
			geoPoints.add(new GeoPoint(
					Integer.parseInt(objectFields[(i*2)+1]),
					Integer.parseInt(objectFields[(i*2)+2])));
		}
	}

	public GeoPoint getAnimateTo() {
		if (geoPoints.size() == 0) {
			return null;
		}
		return geoPoints.get(geoPoints.size()-1);
	}
}
